package com.xckj.cycling.presenter;

import androidx.annotation.NonNull;

import com.xckj.cycling.bean.BaseBean;
import com.xckj.cycling.bean.DeviceAddedResponseBean;
import com.xckj.cycling.framework.net.RetrofitManager;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.iview.IBindDeviceView;
import com.xckj.cycling.iview.IDeviceAddedView;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: ChenYao
 * @Describe: 绑定设备
 * @Date: 2023/3/18 16:15
 */
public class BindDevicePresenter {
    private final IBindDeviceView bindDeviceView;

    public BindDevicePresenter(IBindDeviceView bindDeviceView) {
        this.bindDeviceView = bindDeviceView;
    }

    public void bindDevice(String mac, String name) {
        bindDeviceView.showLoading();
        Call<BaseBean<String>> call = RetrofitManager.getInstance().getService().bindDevice(mac, name);
        call.enqueue(new Callback<BaseBean<String>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<String>> call, @NonNull Response<BaseBean<String>> response) {
                if (response.isSuccessful()) {
                    BaseBean<String> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        bindDeviceView.bind_device_success(mac);
                    } else {
                        bindDeviceView.bind_device_failure(baseBean.msg);
                    }
                } else {
                    bindDeviceView.bind_device_failure("request failed");
                }
                bindDeviceView.hideDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<String>> call, @NonNull Throwable t) {
                t.printStackTrace();
                bindDeviceView.bind_device_failure("request failed");
                bindDeviceView.hideDialog();
            }
        });
    }
}
